package com.zoncont.web.controller.dev;

import com.zoncont.common.annotation.Log;
import com.zoncont.common.core.controller.BaseController;
import com.zoncont.common.core.domain.AjaxResult;
import com.zoncont.common.core.page.TableDataInfo;
import com.zoncont.common.enums.BusinessType;
import com.zoncont.common.utils.DateUtils;
import com.zoncont.estate.domain.Company;
import com.zoncont.estate.service.ICompanyService;
import com.zoncont.framework.util.ShiroUtils;
import com.zoncont.estate.domain.DevInfo;
import com.zoncont.estate.service.IDevInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 设备 信息操作处理
 *
 * @author ruoyi
 * @date 2019-08-04
 */
@Controller
@RequestMapping("/dev/devInfo")
public class DevInfoController extends BaseController
{
    private String prefix = "devInfo";

	@Autowired
	private IDevInfoService devInfoService;


	@Autowired
	private ICompanyService companyService;



	@RequiresPermissions("devMenu:devInfo:view")
	@GetMapping()
	public String devInfo( ModelMap mmap)
	{
		List<Company> companyList=companyService.selectCompanyList(new Company());

		mmap.put("companyList",companyList);

	    return prefix + "/devInfo";
	}




	/**
	 * 查询设备列表
	 */
	@RequiresPermissions("devMenu:devInfo:view")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DevInfo devInfo)
	{
		return getList(devInfo);
	}

	public TableDataInfo getList(DevInfo devInfo){

		List<Company> companyList=companyService.selectCompanyList(new Company());

		List<DevInfo> list = devInfoService.selectDevInfoList(devInfo);

		if(list!=null) {
			list.stream().forEach(item -> {

				if(companyList!=null) {
					for (int i=0;i<companyList.size();i++) {
						Company company =  companyList.get(i);
						if (company!=null && company.getCompId() !=null && company.getCompId().equals(item.getCompanyId())) {

							item.setCompanyName(company.getOrgName());
						}
					}
				}

			});
		}
		startPage();
		return getDataTable(list);
	}




	/**
	 * 新增设备
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap )
	{
		List<Company> companyList=companyService.selectCompanyList(new Company());
		mmap.put("companyList",companyList);
	    return prefix + "/add";
	}

	/**
	 * 新增保存设备
	 */
	@RequiresPermissions("devMenu:devInfo:view")
	@Log(title = "设备", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DevInfo devInfo)
	{
		devInfo.setCreateTime(DateUtils.getTime());
		devInfo.setCreateName(ShiroUtils.getLoginName());
		devInfo.setDevCode(devInfo.getDevId());
		return toAjax(devInfoService.insertDevInfo(devInfo));
	}

	/**
	 * 修改设备
	 */
	@GetMapping("/edit/{infoId}")
	public String edit(@PathVariable("infoId") Integer infoId, ModelMap mmap)
	{
		 return  editDev(infoId, mmap,prefix + "/edit");
	}



	private  String   editDev(Integer infoId, ModelMap mmap, String  page){
		DevInfo devInfo = devInfoService.selectDevInfoById(infoId);
		List<Company> companyList=companyService.selectCompanyList(new Company());

		String types=devInfo.getBelongType();



		mmap.put("companyList",companyList);
		mmap.put("devInfo", devInfo);

		return page;
		//return prefix + "/edit";
	}

	/**
	 * 修改保存设备
	 */
	@RequiresPermissions("devMenu:devInfo:view")
	@Log(title = "设备", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DevInfo devInfo)
	{


		return toAjax(devInfoService.updateDevInfo(devInfo));
	}

	/**
	 * 删除设备
	 */
	@RequiresPermissions("devMenu:devInfo:view")
	@Log(title = "设备", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(devInfoService.deleteDevInfoByIds(ids));
	}




}
